package com.cherish.meeting.api.controller;


import com.cherish.meeting.api.service.IMeetingRecordService;
import com.cherish.meeting.common.api.HttpResult;
import com.cherish.meeting.common.constant.MeetingStatus;
import com.cherish.meeting.common.util.CalendarUtil;
import com.cherish.meeting.common.util.StringUtil;
import com.cherish.meeting.db.entity.MeetingRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-03-02
 */
@Slf4j
@RestController
@RequestMapping("/meetingRecord")
public class MeetingRecordController {

    @Autowired
    private IMeetingRecordService meetingRecordService;

    @GetMapping("/getMeetingByRoom")
    public HttpResult getMeetingByRoom(HttpServletRequest request) throws ParseException {

        StringBuffer msg = new StringBuffer();

        //会议相关
        String roomArea = request.getParameter("qualificationName");
        String roomNo = request.getParameter("categoryBrandName");

        if (StringUtil.isEmpty(roomArea)) {
            msg.append("& 请选择职场");
        }
        if (StringUtil.isEmpty(roomNo)) {
            msg.append("& 请选择会议室");
        }

        if (StringUtil.isNotEmpty(msg.toString())) {
            return HttpResult.failed(msg.toString().replaceFirst("& ", ""));
        }

        MeetingRecord record = new MeetingRecord();
        record.setRoomArea(roomArea).setRoomNo(roomNo);

        List<MeetingRecord> records = meetingRecordService.getMeetingByRoom(record);

        if (null != records && records.size() > 0) {
            return HttpResult.success("预订记录查询成功").setData(records);
        } else {
            return HttpResult.failed("当前会议室没有预订记录");

        }
    }

    @GetMapping("/getMyMeetings")
    public HttpResult getMyMeetings(HttpServletRequest request) {
        String openid = request.getParameter("openid");

        StringBuffer msg = new StringBuffer();

        if (StringUtil.isEmpty(openid)) {
            msg.append("& openid获取失败");
        }

        if (StringUtil.isNotEmpty(msg.toString())) {
            return HttpResult.failed(msg.toString().replaceFirst("& ", ""));
        }

        List<MeetingRecord> records = meetingRecordService.getMyMeetings(openid);
        if (null != records && records.size() > 0) {
            return HttpResult.success("预订记录查询成功").setData(records);
        } else {
            return HttpResult.failed("您当前没有预定记录");

        }
    }

    @GetMapping("/makeMeeting")
    public HttpResult makeMeeting(HttpServletRequest request) throws ParseException {

        StringBuffer msg = new StringBuffer();

        //会议相关
        String roomArea = request.getParameter("qualificationName");
        String roomNo = request.getParameter("categoryBrandName");
        String date = request.getParameter("date");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String title = request.getParameter("title");

        //用户相关
        String openid = request.getParameter("openid");

        if (StringUtil.isEmpty(roomArea)) {
            msg.append("& 请选择职场");
        }
        if (StringUtil.isEmpty(roomNo)) {
            msg.append("& 请选择会议室");
        }
        if (StringUtil.isEmpty(title)) {
            msg.append("& 请输入会议主题");
        }
        if (StringUtil.isEmpty(openid)) {
            msg.append("& 未能获取到openid");
        }

        if (StringUtil.isNotEmpty(msg.toString())) {
            return HttpResult.failed(msg.toString().replaceFirst("& ", ""));
        }

        MeetingRecord record = new MeetingRecord();
        record.setRoomArea(roomArea)
                .setRoomNo(roomNo)
                .setMeetingDate(CalendarUtil.strToDate(date))
                .setStartTime(CalendarUtil.strToTime(startTime))
                .setEndTime(CalendarUtil.strToTime(endTime))
                .setTitle(title)
                .setOpenid(openid);

        log.info("保存会议记录 >>> 接收到会议记录：{}", record.toString());

        String result = meetingRecordService.bookMeeting(record);

        if (MeetingStatus.BOOK_SUCCESS.equals(result)) {
            return HttpResult.success("会议室预订成功");
        } else {
            return HttpResult.failed(result);
        }

    }

}
